package com.topnav.mc.admin.entity.wrapper;

import com.topnav.mc.admin.entity.param.FenceAlarmParam;
import com.topnav.mc.base.mc.McWrapper;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * @Author: kenny
 * @Date: 2023/11/13 11:15
 */
public class FenceAlarmWrapper extends McWrapper<FenceAlarmParam> {
    public FenceAlarmWrapper(FenceAlarmParam entity) {
        super(entity);
        if (this.permissionGrade != null ){
            if (this.permissionGrade.getPermissionLevel() == 1){
                this.eq("d.user_id",this.permissionGrade.getUserId());
            }else if (this.permissionGrade.getPermissionLevel() == 2){
                this.eq("d.org_id",this.permissionGrade.getOrgId());
            }else if (this.permissionGrade.getPermissionLevel() == 3 || this.permissionGrade.getPermissionLevel() == 4){
                this.likeRight("o.path",this.permissionGrade.getOrgPath());
            }
        }
        if (entity != null){

            if (entity.getId()!=null){this.eq("fa.id",entity.getId());}
            if (entity.getFenceAlarmType()!=null){this.eq("fa.alarmType",entity.getFenceAlarmType());}
            if (entity.getDeviceId()!=null){this.eq("f.device_id",entity.getDeviceId());}
            if (entity.getFenceId()!=null){this.eq("fa.fence_id",entity.getFenceId());}
            if (entity.getOrgId()!=null){this.eq("f.org_id",entity.getOrgId());}
            if (entity.getUserId()!=null){this.eq("d.user_id",entity.getUserId());}

            if (entity.getFrom()!=null){this.ge("fa.alarm_time",entity.getFrom());}
            if (entity.getTo()!=null){this.le("fa.alarm_time",entity.getTo());}

            if (!StringUtils.isEmpty(entity.getKeyword())){
                this.and(wrapper -> wrapper.like("f.fence_name", entity.getKeyword())
                        .or().like("f.note", entity.getKeyword())
                );
            }
            if (!StringUtils.isEmpty(entity.getFenceName())){
                this.like("f.fence_name", entity.getFenceName());
            }
            this.orderByDesc("fa.id");
        }
    }
}
